package Controllers;


import Bean.BeanSc;
import Bean.BeanStudent;
import Impl.ScServiceImpl;
import com.alibaba.fastjson.JSONArray;
import exception.BooksNotFoundException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

@Controller
public class ShowScController extends HttpServlet {

    private static final long serialVersionUID = 1L;
    ScServiceImpl scService;
    @RequestMapping("/ShowSc")
    protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        JSONArray arr = new JSONArray();
        ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
        scService=(ScServiceImpl) applicationContext.getBean("ScServiceImpl");
        Collection<BeanSc> scs;
        int class_id=Integer.parseInt(request.getParameter("class_id").replaceAll("'", ""));
        System.out.println(class_id);
        String date=request.getParameter("day").replaceAll("'", "");
        System.out.println(date);
        BeanSc sc=new BeanSc();
        sc.setClass_id(class_id);
        sc.setDay(date);
        try {
            scs = scService.getSc(sc);
            for(BeanSc beanSc:scs) {
                arr.add(beanSc.toJson());
            }
        } catch (BooksNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.getWriter().append(arr.toJSONString());
    }
}
